using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraBars;
using System.Collections.ObjectModel;
using YidanSoft.FrameWork.WinForm;

namespace YidanSoft.MainFram
{
    internal class PluginMenuBar:Bar, IPluginMenuBar
    {
        Collection<IPlugInMenuInfo> _items;
        object _tag;

        public object Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        public Collection<IPlugInMenuInfo> Items
        {
            get 
            {
                if (_items == null)
                {
                    _items = new Collection<IPlugInMenuInfo>();
                }
                return _items;
            }
        }

        void GenItems(Bar bar)
        {
            _items = new Collection<IPlugInMenuInfo>();
            if (bar == null) return;
            for (int i = 0; i < bar.ItemLinks.Count; i++)
            {
                BarItem baritem = bar.ItemLinks[i].Item;
                IPlugInMenuInfo pmi = baritem as IPlugInMenuInfo;
                if (pmi != null) _items.Add(pmi);
            }
        }

        public string Name 
        {
            get { return BarName; }
            set { BarName = value; }
        }

        public PluginMenuBar():this(null)
        {
        }

        public PluginMenuBar(Bar bar):base()
        {
            GenItems(bar);
        }

        public override BarItemLink AddItem(BarItem item)
        {
            if (item is IPlugInMenuInfo) _items.Add(item as IPlugInMenuInfo);
            return base.AddItem(item);
        }

        public override void AddItems(BarItem[] items)
        {
            for (int i = 0; i < items.Length; i++)
                if (items[i] is IPlugInMenuInfo) _items.Add(items[i] as IPlugInMenuInfo);
            base.AddItems(items);
        }
    }

    public interface IPluginMenuBar
    {
        string Name { get;set;}
        Collection<IPlugInMenuInfo> Items { get;}
    }
}
